<?php

/**
 * This is the model class for table "{{post}}".
 *
 * The followings are the available columns in table '{{post}}':
 * @property integer $id
 * @property integer $key
 * @property string $title
 * @property string $url
 * @property string $phone
 * @property string $thumbnail
 * @property string $content
 * @property integer $cat_id
 * @property integer $user_id
 * @property integer $price_range_id
 * @property integer $district_id
 * @property integer $children_cat_id
 * @property integer $price
 * @property integer $created_at
 * @property integer $start_time
 * @property integer $end_time
 * @property integer $type
 * @property integer $status
 * @property integer $google_index
 */
class Post extends MyARContent
{

    const TYPE_NORMAL = 1;
    const TYPE_NEWS   = 0;

    public $verifyCode;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Post the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{post}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('title, url,content', 'required'),
            array('title, url', 'unique', 'on' => 'save'),
            array('count,rank,cat_id,created_at, type, status', 'numerical', 'integerOnly' => true),
            array('title, url,meta_desc,meta_keyword,seo_title', 'length', 'max' => 250),
            array('title,url', 'unique', 'on' => 'unni'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id,rank, title,seo_title, google_index, url, thumbnail, content, cat_id,  type, status,description', 'safe',),
            array('id, title, url,seo_title, thumbnail, content, cat_id,  type, status,description', 'safe', 'on' => 'search'),
//            array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements(), 'on' => 'useCaptcha'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        $relations             = parent::relations();
        $relations['category'] = array(self::BELONGS_TO, 'Category', 'cat_id');
        return $relations;
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        $array = array(
            'id'           => 'ID',
            'key'          => 'Key',
            'title'        => 'Title',
            'url'          => 'Url',
            'thumbnail'    => 'Thumbnail',
            'content'      => 'Content',
            'description'  => 'Description',
            'count'        => 'Count',
            'cat_id'       => 'Cat',
            'created_at'   => 'Created_at',
            'type'         => 'Type',
            'rank'         => 'Rank',
            'status'       => 'Status',
            'meta_desc'    => 'Meta_desc',
            'meta_keyword' => 'Meta_keyword',
            'verifyCode'   => 'Verify Code',
            'google_index'   => 'Google',
        );
        foreach ($array as $key => $val)
        {
            $array[$key] = Yii::t('sub_all', $val);
        }
        return $array;
    }

    public static function getArrayType()
    {
        return array(
            self::TYPE_NORMAL => ' Bài viết thường ',
            self::TYPE_NEWS   => 'Tin tức',
        );
    }

    public function getTypeLabel()
    {
        if (!$this->hasAttribute("type"))
            return '';

        $labelClass = 'label ';
        switch ($this->type)
        {
            case self::TYPE_NEWS:
                $labelClass .= 'label-warning';
                break;

            case self::TYPE_NORMAL:
                $labelClass .= 'label-danger';
                break;

            default:
                break;
        }
        $labels = self::getArrayType();
        $label  = isset($labels[$this->type]) ? $labels[$this->type] : $this->type;
        return "<span class='$labelClass'>$label</span>";
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('key', $this->key);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('url', $this->url, true);
        $criteria->compare('phone', $this->phone, true);
        $criteria->compare('thumbnail', $this->thumbnail, true);
        $criteria->compare('content', $this->content, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('count', $this->count);
        $criteria->compare('cat_id', $this->cat_id);
        $criteria->compare('user_id', $this->user_id);
        $criteria->compare('price_range_id', $this->price_range_id);
        $criteria->compare('district_id', $this->district_id);
        $criteria->compare('children_cat_id', $this->children_cat_id);
        $criteria->compare('price', $this->price);
        $criteria->compare('created_at', $this->created_at);
        $criteria->compare('start_time', $this->start_time);
        $criteria->compare('end_time', $this->end_time);
        $criteria->compare('type', $this->type);
        $criteria->compare('status', $this->status);
        $criteria->compare('google_index', $this->google_index);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function getPublicPhoto($width, $height)
    {
        if ($this->thumbnail)
        {

            return $this->resizePhoto($this->thumbnail, $width, $height);
        }
        else
        {
            return $this->resizePhoto(Yii::app()->baseUrl . '/files/no_thumbnail.png', $width, $height);
        }
    }

    public function getPublicUrl($absolute = false)
    {
        if ($absolute == FALSE)
        {
            return Yii::app()->createUrl("post/view", array(
                        'url' => $this->url,
            ));
        }
        else
        {
            return Yii::app()->createAbsoluteUrl("post/view", array(
                        'url' => $this->url,
            ));
        }
    }

    /**
     * Lấy các phần tử Bài viết thành một mảng
     * 
     */
    public static function getArrayPost()
    {
        $criteria            = new CDbCriteria;
        $criteria->condition = "status > 0";
        $criteria->params    = array(
        );
        $criteria->order     = 'id DESC';
        $allPublic           = self::model()->findAll($criteria);
        if ($allPublic != NULL)
        {
            foreach ($allPublic as $item)
            {
                $array[$item->id] = $item->title;
            }
        }
        if ($array)
        {
            return $array;
        }
        else
        {
            return array();
        }
    }

    public function getTitleSeo()
    {
        if ($this->seo_title)
        {
            return $this->seo_title;
        }
        else
        {
            return $this->title;
        }
    }

}
